package com.zmm.myyimixian.ui.activity.address;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseActivity;
import com.zmm.myyimixian.db.Address;
import com.zmm.myyimixian.db.DataDao;
import com.zmm.myyimixian.ui.adapter.AddressAdapter;
import com.zmm.myyimixian.util.UIManager;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.listener.FindListener;

public class AddressActivity extends BaseActivity {
    @BindView(R.id.address_toolbar)
    Toolbar addressToolbar;//toolbar
    @BindView(R.id.address_progress)
    ProgressBar addressProgress;
    @BindView(R.id.address_lv)
    ListView addressLv;
    @BindView(R.id.address_llayout_null)
    LinearLayout addressLlayoutNull;//空视图
    private AddressAdapter mAdapter;

    @Override
    protected void initView() {
        //返回
        addressToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("cart".equals(getIntent().getStringExtra("cart"))) {
                    //回传地址
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    finish();
                }
            }
        });
        mAdapter = new AddressAdapter();
        addressLv.setAdapter(mAdapter);
        addressLv.setEmptyView(addressLlayoutNull);
        addressLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Address item = (Address) mAdapter.getItem(position);
                if ("cart".equals(getIntent().getStringExtra("cart"))) {
                    //回传地址
                    Intent intent = new Intent();
                    intent.putExtra("address", item);
                    setResult(RESULT_OK, intent);
                    finish();
                }

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_address;
    }

    @Override
    protected void onResume() {
        super.onResume();
        addressProgress.setVisibility(View.VISIBLE);
        DataDao.getAllAddress(new FindListener<Address>() {
            @Override
            public void onSuccess(List<Address> list) {
                addressProgress.setVisibility(View.GONE);
                addressLlayoutNull.setVisibility(View.VISIBLE);
                mAdapter.setData(list);
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(AddressActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                addressProgress.setVisibility(View.GONE);
                addressLlayoutNull.setVisibility(View.VISIBLE);
            }
        });
    }

    @OnClick({R.id.address_add, R.id.address_llayout_null})
    public void onClick(View view) {
        //跳转到新建收获地址
        UIManager.startNewAddressActivity(AddressActivity.this);
    }

}
